package com.bjx.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.system.domain.entity.PostEntity;
import com.bjx.system.domain.query.PostQuery;
import com.bjx.system.domain.vo.PostVO;
import com.bjx.system.mapper.PostMapper;
import com.bjx.system.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc: 职位服务实现
 * @author: liushuai
 * @date: 2022/10/15
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, PostEntity> implements IPostService {
    @Autowired
    private PostMapper postMapper;

    @Override
    public IPage<PostVO> listPostPage(PostQuery postQuery) {
        IPage<PostVO> iPage = postMapper.listPostPage(postQuery.getPage(),postQuery);
        return iPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertPost(PostVO postVO) {
        PostEntity entity = Convert.convert(PostEntity.class, postVO);
        postMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePost(PostVO postVO) {
        PostEntity entity = Convert.convert(PostEntity.class, postVO);
         postMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePostByPostId(Long postId) {
        postMapper.deleteById(postId);
    }

    @Override
    public PostVO selectPostByPostId(Long postId) {
        PostEntity entity = postMapper.selectById(postId);
        PostVO vo = Convert.convert(PostVO.class,entity);
        return vo;
    }

    @Override
    public List<PostVO> listPostAll() {
        List<PostEntity> postEntities = postMapper.selectList(new LambdaQueryWrapper<>());
        List<PostVO> posts = Convert.toList(PostVO.class, postEntities);
        return posts;
    }


}
